import update from 'immutability-helper';
import Storage from '@doctorwork/utils/storage';
// 通过wxstorage存储
const storage = new Storage();

export default {
    namespace: '$storage',
    state: {
        keys: []
    },
    reducers: {
        // 同步到内存中
        save(state, { payload }) {
            const { key, value } = payload;
            return update(state, { $merge: { [key]: value } });
        },
        sync(state, { payload }) {
            const { key, value } = payload;
            if (!state[key]) {
                return update(state, { $merge: { [key]: value } });
            }
            return state;
        }
    },
    effects: {
        *set({ payload }, { put }) {
            yield put({ type: 'save', payload });
            return storage.set(payload);
        },
        *get({ payload }) {
            return storage.get(payload);
        },
        *all () {
            return storage.all();
        },
        *some ({payload}) {
            return storage.some(payload);
        }
    }
};
